<?php

/**
 * Created by IntelliJ IDEA.
 * User: luwei
 * Date: 2018/2/28
 * Time: 10:22
 */

namespace app\modules\mch\controllers\purchase;

use app\hejiang\ApiCode;
use app\models\DistrictArr;
use app\models\Mch;
use app\models\MchCommonCat;
use app\models\User;
use app\models\PurchaseApply;
use app\models\Purchase;

use app\modules\mch\models\purchase\PurchaseApplyForm;
use app\modules\mch\models\purchase\PurchaseForm;


class IndexController extends Controller
{
    public function actionIndex()
    {
        $form = new PurchaseForm();
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
        $res = $form->search();
        return $this->render('index', [
            'list' => $res['list'],
            'pagination' => $res['pagination'],
            'get' => \Yii::$app->request->get(),
        ]);
    }

    public function actionApply($status = 0)
    {
		
        $form = new PurchaseApplyForm();
		
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
	    $form->status = $status;
        $res = $form->search();
        return $this->render('apply', [
            'list' => $res['list'],
            'pagination' => $res['pagination'],
            'get' => \Yii::$app->request->get(),
        ]);
    }

    public function actionEdit($id)
    {
        $model = PurchaseApply::findOne([
            'id' => $id,
            'store_id' => $this->store->id,
        ]);
        if (!$model) {
            \Yii::$app->response->redirect(\Yii::$app->request->referrer)->send();
            return;
        }
        if (\Yii::$app->request->isPost) {
            $form = new PurchaseApplyForm();
            $form->model = $model;
            $form->attributes = \Yii::$app->request->post();
            return $form->save();
        } else {
            foreach ($model as $index => $value) {
                $model[$index] = str_replace("\"", "&quot;", $value);
            }
            return $this->render('edit', [
                'model' => $model
            ]);
        }
    }
	
	    /**
     * 店铺状态开头
     * @param $id 多商户 ID
     * @param $status 状态 1 或 0
     * @param $type 要改状态的字段
     * @return array
     */
    public function actionSetStatus($id, $status)
    {
        $mch = Purchase::findOne([
            'id' => $id,
            'store_id' => $this->store->id,
        ]);
        if (!$mch) {
            return [
                'code' => 1,
                'msg' => '团长不存在。',
            ];
        }
        $mch->status = $status ? 1 : 0;
        $mch->save();
        return [
            'code' => 0,
            'msg' => '保存成功。',
        ];
    }
	
	 public function actionMchSetting($id = null)
    {
         $model = Purchase::findOne([
            'store_id' => $this->store->id, 
        ]);
        if(!$model) {
            $model = new Purchase();
        }
        if(\Yii::$app->request->isPost){
            $form = new PurchaseForm();
            $form->attributes = \Yii::$app->request->post();
            $form->model = $model;
		
            $form->is_share = \Yii::$app->request->post('is_share');
            $form->num = \Yii::$app->request->post('num');
            $form->store_id = $this->store->id;
            return $form->save();
        }

        return $this->render('mch-setting', [
            'setting' => $model,
        ]);
    }

   

   

    /**
     * 销售报表
     */
    public function actionReportForms()
    {
        $model = new ReportFormsForm();
        $excelFields = $model->excelFields();
        $model->attributes = \Yii::$app->request->get();
        $model->attributes = \Yii::$app->request->post();
        $list = $model->search();

        return $this->render('report-forms', [
            'data' => $list,
            'exportList' => \Yii::$app->serializer->encode($excelFields)
        ]);
    }
}
